/******************************************************************************
 * File: : Room.cpp
 * Autor : Thomas Pfeiffer
 * Descr.: Implementation of the class Room
 *****************************************************************************/
#include "Room.h"
#include "Side.h"

// Copy Constructor: Copies all the sides and
// sets the right parent for them
Room::Room( const Room &r ) : mRoomnumber(0)
{
	mTop = r.getTop()->clone();
	mRight = r.getRight()->clone();
	mBottom = r.getBottom()->clone();
	mLeft = r.getLeft()->clone();

	mTop->setParent(this);
	mRight->setParent(this);
	mBottom->setParent(this);
	mLeft->setParent(this);
}

// Constructor of the Room. 
// params: Pointers to the sides of the room
Room::Room (Side * top, Side * right, Side * bottom, Side * left)
{
	mTop = top;
	mRight = right;
	mBottom = bottom;
	mLeft = left;

	mTop->setParent(this);
	mRight->setParent(this);
	mBottom->setParent(this);
	mLeft->setParent(this);
}

// Destructor: Free all sides and set the pointers to 0
Room::~Room()
{
	delete mTop; mTop = 0;
	delete mRight; mRight = 0;
	delete mBottom; mBottom = 0;
	delete mLeft; mLeft = 0;
}

// set the roomnumber
void Room::setIndex( size_t rnr )
{
	mRoomnumber = rnr;
}

// return the roomnumber
size_t Room::getIndex() const
{
	return mRoomnumber;
}

// Prints a room. (Calls the printfunctions of the sides.)
// param printsingle: when true, the room prints it's top 
// side even if it has a top neighbour. When this function
// is being called by Roomplan, printsingle is always false
void Room::Print(bool printsingle) const
{
  // Don't print top if top side has a neighbour
  // except printsingle is set to true
	if (printsingle || !mTop->hasNeighbour()) {
  
    // Call print for every position of the top side
		for (size_t i = 0; i < mTop->getLength(); ++i) {
			mTop->Print(i);
		}
		std::cout << std::endl;
	}
	for (size_t i = 0; i < mLeft->getLength(); ++i) {
		mLeft->Print(i); // Print the Symbol for the left wall
		
    // Print the inside of the room ( 1 line)
		for(size_t j = 1; j < mTop->getLength() - 1; ++j) {
			std::cout << " ";
		}
    
    mRight->Print(i); // Print the Symbol for the right wall
		std::cout << std::endl; // Go to the next line
	}

  // Call print for every position of the bottom side
	for (size_t i = 0; i < mBottom->getLength(); ++i) {
		mBottom->Print(i);
	}
	std::cout << std::endl;
}

///////////////////////////////////////
// Getters and setters for the sides //
///////////////////////////////////////
Side* Room::getTop() const
{
	return mTop;
}

Side* Room::getRight() const
{
	return mRight;
}

Side* Room::getBottom() const
{
	return mBottom;
}

Side* Room::getLeft() const
{
	return mLeft;
}

void Room::setTop( Side*  s )
{
	delete mTop;
	mTop = s;
	mTop->setParent(this);
}

void Room::setRight( Side* s )
{
	delete mRight;
	mRight = s;
	mRight->setParent(this);
}

void Room::setBottom( Side* s )
{
	delete mBottom;
	mBottom = s;
	mBottom->setParent(this);
}

void Room::setLeft( Side* s )
{
	delete mLeft;
	mLeft = s;
	mLeft->setParent(this);
}

// sets the width and height of a room /
// the length of the sides
void Room::setDimension( size_t width, size_t height )
{
	mTop->setLength(width);
	mRight->setLength(height);
	mBottom->setLength(width);
	mLeft->setLength(height);
}